﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BestCode.Creds.Business;
using BestCode.Creds.Entities;
using BestCode.Wpf.ViewModel.Data;

namespace BestCode.Creds.UI.ViewModel
{
    public class VmFieldDefinition : DataModelBase<FieldDefinition>
    {
        private EntityInfo currentEntity = null;
        private FieldDefinitionBusiness business = null;
        public VmEntityChild VmEntityChild { get; set; }
        
        private EntityChild selectedEntityChild;
        public EntityChild SelectedEntityChild
        {
            get { return selectedEntityChild; }
            set
            {
                selectedEntityChild = value;
                if (selectedEntityChild != null)
                {
                    if (selectedEntityChild.Id == -1)
                    {
                        Items = new ObservableCollection<FieldDefinition>(
                            business.GetByEntityId(currentEntity.Id));
                        Total = Items.Count;
                        EnabledItemsCount = Items.Count(p => p.IsEnabled);
                    }
                    else
                    {
                        Items =
                            new ObservableCollection<FieldDefinition>(
                                business.GetByEntityId(currentEntity.Id)
                                    .Where(p => p.EntityChild != null && p.EntityChild.Id == selectedEntityChild.Id));
                        Total = Items.Count;
                        EnabledItemsCount = Items.Count(p => p.IsEnabled);
                    }

                }
            }
        }

        public string OutputFolder
        {
            get { return Config.ConfigReader.Current.Config.DefaultOutputFolder; }
        }

        public string PicturesFolder
        {
            get { return currentEntity.PicturesFolder;}

        }

        private int enabledItemsCount;
        public int EnabledItemsCount
        {
            get { return enabledItemsCount; }
            set { enabledItemsCount = value; FirePropertyChanged("EnabledItemsCount"); }
        }


        private int total;
        public int Total
        {
            get { return total; }
            set { total = value; FirePropertyChanged("Total"); }
        }


        public VmFieldDefinition(EntityInfo entity)
        {
            currentEntity = entity;
            business = new FieldDefinitionBusiness();
            SetDataService(business);

            GetFieldsByEntity(currentEntity);
            LoadChildrenEntities();

        }
      

        public void LoadChildrenEntities()
        {
            VmEntityChild = new VmEntityChild();
            VmEntityChild.LoadByEntityId(currentEntity.Id);
            var allEntityChild = new EntityChild()
            {
                Id = -1,
                Name = "All",
                Entity = currentEntity,
                PicturesFolder = currentEntity.PicturesFolder
            };

            VmEntityChild.ChildrenByEntity.Add(allEntityChild);
            SelectedEntityChild = allEntityChild;

        }

        private void GetFieldsByEntity(EntityInfo currentEntity)
        {
            Items = business.GetByEntityId(currentEntity.Id);
        }


        internal void SearchByFilter(string filter)
        {
            if (SelectedEntityChild.Id > 0)
            {
                var datas =
                    business.GetByEntityId(currentEntity.Id)
                        .Where(
                            p =>
                                p.EntityChild.Id == SelectedEntityChild.Id &&
                                (p.FirstName.ToLower().Contains(filter.ToLower()) || (p.LastName1.ToLower().Contains(filter.ToLower()))));

                Items = new ObservableCollection<FieldDefinition>(datas);
            }
            else
            {
                var datas =
                    business.GetByEntityId(currentEntity.Id)
                        .Where(p => p.FirstName.ToLower().Contains(filter.ToLower()) || (p.LastName1.ToLower().Contains(filter.ToLower())));

                Items = new ObservableCollection<FieldDefinition>(datas);
            }
        }
    }
}
